//
//  MagazineCategoryWidget.m
//  JianChaRiBao
//
//  Created by yuzhou on 13-5-21.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "MagazineCategoryWidget.h"
#import "MagazineCategoryWidgetCell.h"
#import "AppDelegate.h"

@implementation MagazineCategoryWidget

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor darkGrayColor];
        categoryTableView = [[[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain] autorelease];
//        if ([[UIDevice currentDevice].systemVersion intValue] >= 7.0) {
//            categoryTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
//        }
        categoryTableView.dataSource = self;
        categoryTableView.delegate = self;
        categoryTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        categoryTableView.backgroundColor = [UIColor clearColor];
        categoryTableView.separatorColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        [self addSubview:categoryTableView];
    }
    return self;
}

- (void)updateCategory:(NSArray *)categoryArray key:(NSString *)name
{
    self.categoryArray = categoryArray;
    self.nameStr = name;
    [categoryTableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _categoryArray.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentify = @"MagazineCategoryWidgetCell";
    MagazineCategoryWidgetCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentify];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MagazineCategoryWidgetCell" owner:self options:nil] objectAtIndex:0];
    }
    cell.nameLabel.text = indexPath.row == 0 ? @"全部分类" : [[_categoryArray objectAtIndex:indexPath.row - 1] objectForKey:self.nameStr];

//    cell.nameLabel.text = [[_categoryArray objectAtIndex:indexPath.row] objectForKey:self.nameStr];
    cell.contentView.backgroundColor = [UIColor darkGrayColor];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray * array = indexPath.row == 0 ? nil : [_categoryArray objectAtIndex:indexPath.row - 1];
    if ([self.nameStr isEqualToString:@"name"]) {
//        NSArray * array = [_categoryArray objectAtIndex:indexPath.row];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"CategoryCellDidSelect" object:array];
    }else{
//        NSArray * array = [_categoryArray objectAtIndex:indexPath.row];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NovelCategoryCellDidSelect" object:array];
    }
    [[UIApplication rootViewController] hideMenu];
}

- (void)dealloc
{
    [_categoryArray release];
    [categoryTableView release];
    [super dealloc];
}

@end
